//
//  AppDelegate.m
//  zoneTry
//
//  Created by youminuo on 16/7/4.
//  Copyright © 2016年 ZoneTry. All rights reserved.
//

#import "AppDelegate.h"
#import "GuideViewController.h"
#import "MainTabBarController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:kScreenCGRect];
    
    self.window.rootViewController = [self pickViewController];
    
    [self.window makeKeyAndVisible];
    
    [self saveAppVersion];

    return YES;
}

// 选择应该显示的控制器
- (UIViewController*)pickViewController
{
    if ([[self getInfoAppVersion] isEqualToString:[self getSavedAppVersion]]) {
        // 如果相等 进入正常的tabbarvc
        MainTabBarController *tab = [[MainTabBarController alloc] init];
        return tab;
    }
    else {
        // 如果不相等 显示新特性页面
        GuideViewController *vc = [[GuideViewController alloc] init];
        return vc;
    }
}

// 获取当前的版本号(info.plist)
- (NSString*)getInfoAppVersion
{
    NSDictionary *infoDict = [NSBundle mainBundle].infoDictionary;
    NSString *appVersion = infoDict[@"CFBundleShortVersionString"];
    return appVersion;
}

// 查询当初保存的版本号
- (NSString*)getSavedAppVersion
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"appVersion"];
}

// 保存当前的版本号
- (void)saveAppVersion
{
    NSDictionary *infoDict = [NSBundle mainBundle].infoDictionary;
    NSString *appVersion = infoDict[@"CFBundleShortVersionString"];
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:appVersion forKey:@"appVersion"];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
